package com.example.demo.Service;

import com.example.demo.Bean.LeavePeople;
import com.example.demo.Bean.LogDate;
import com.example.demo.Bean.People;
import com.example.demo.Mapper.LeavePeopleMapper;
import com.example.demo.Mapper.PeopleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;


@Service("peopleService")
public class PeopleService {
    @Autowired
    private PeopleMapper peopleMapper;
    @Autowired
    private LogDateService logDateService;
    @Autowired
    private LeavePeopleMapper leavePeopleMapper;

    public List<People> selectPeople(){
        return peopleMapper.selectPeople();
    }
    public People selectOnePeople(String peopleID){return peopleMapper.selectOnePeople(peopleID);}
    public void updateOnePeople(People people){
        LogDate log = new LogDate();
        log.setType(" ");
        People pp = selectOnePeople(people.getPeopleID());
        if(!pp.equals(people)) {
            if (!pp.getComeTime().equals(people.getComeTime())) log.setType(log.getType().concat(" !修改入职时间"));
            if (!pp.getFace().equals(people.getFace())) log.setType(log.getType().concat(" !修改政治面貌"));
            if (pp.getId() != people.getId()) log.setType(log.getType().concat(" !更换部门"));
            if (pp.getMoney() != (people.getMoney())) log.setType(log.getType().concat(" !修改工资"));
            if (!pp.getLearn().equals(people.getLearn())) log.setType(log.getType().concat(" !修改学历"));
            if (!pp.getName().equals(people.getName())) log.setType(log.getType().concat(" !修改姓名"));
            if (!pp.getNation().equals(people.getNation())) log.setType(log.getType().concat(" !修改年龄"));
            if (!pp.getWork().equals(people.getWork())) log.setType(log.getType().concat(" !修改职位"));
            if (!pp.getSex().equals(people.getSex())) log.setType(log.getType().concat(" !修改性别"));
        }
        String str = String.valueOf((System.currentTimeMillis()));
        log.setLogID(str);
        log.setDate(new Date());
        log.setWay("被动");
        log.setPeopleID(pp.getPeopleID());
        log.setReason("无");
        logDateService.insertLogDate(log);
        peopleMapper.updateOnePeople(people);
    }
    public void deleteOnePeople(String peopleID){
        People people = peopleMapper.selectOnePeople(peopleID);
        LeavePeople leavePeople = new LeavePeople();
        leavePeople.setPeopleID(peopleID);
        leavePeople.setName(people.getName());
        leavePeople.setComeTime(people.getComeTime());
        leavePeople.setFace(people.getFace());
        leavePeople.setId(people.getId());
        leavePeople.setLearn(people.getLearn());
        leavePeople.setOld(people.getOld());
        leavePeople.setNation(people.getNation());
        leavePeople.setMoney(people.getMoney());
        leavePeople.setWork(people.getWork());
        leavePeople.setSex(people.getSex());
        leavePeople.setLeaveDate(new Date());
        //添加leavePeople
        leavePeopleMapper.insertLeavePeople(leavePeople);
        //删除表中原来的People
        peopleMapper.deleteOnePeople(peopleID);
        //更新日志
        LogDate log = new LogDate();
        log.setLogID(String.valueOf((System.currentTimeMillis())));
        log.setDate(new Date());
        log.setWay("主动");
        log.setPeopleID(peopleID);
        log.setReason("无");
        log.setType("离职");
        logDateService.insertLogDate(log);
    }

}
